﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using taigu.WeChat;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.Extensions.Configuration;
using taigu.WebChat;
using Microsoft.AspNetCore.Authorization;
using taigu.utils;

namespace taigu
{
    public partial class AdminService : ApplicationService, IAdminService
    {

        /// <summary>
        /// 后台帖子管理类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, Route("/admin/community/GetPostCommentList")]
        public ResData<object> GetPostCommentList(PostInfoAdminDto dto)
        {
            try
            {
                List<PostCommentsShowDto> list = (from Comment in _postCommentsRepository
                                                      from user in _weChatUsersRepository
                                                      from post in _postInfoRepository
                                                     
                                                      where user.Id.Equals(Comment.user_id) && Comment.user_id == user.Id && post.Id==Comment.post_id
                                                       select new PostCommentsShowDto
                                                      {
                                                          buId = user.bu_id,
                                                          buName = _organizationStructRepository.Where(x => x.Id.ToString() == user.bu_id).FirstOrDefault().name,
                                                          companyId = user.company_id,
                                                          companyName = _organizationStructRepository.Where(x => x.Id.ToString() == user.company_id).FirstOrDefault().name,
                                                          nickName = user.nick_name,
                                                          realName = user.real_name,
                                                          avatarUrl = user.avatar_url,
                                                          comment_id = Comment.Id.ToString(),
                                                          postImgUrl=imgHost+post.image_url,
                                                          postContent=post.content,
                                                          content = Comment.content,
                                                          CreationTime = Comment.CreationTime,
                                                          status = Comment.status

                                                      }).ToList();
                //if (!string.IsNullOrEmpty(dto.buId))
                //    list = list.Where(x => x.buId == dto.buId);
                //if (!string.IsNullOrEmpty(dto.companyId))
                //    list = list.Where(x => x.companyId == dto.companyId);

                if (dto.status == PostStatus.删除)
                {
                    list = list.Where(x => x.status == PostStatus.删除).ToList();
                }
                if (dto.status == PostStatus.启用)
                {
                    list = list.Where(x => x.status == PostStatus.启用).ToList();
                }

                //if (dto.orderBy == "likes")
                //{
                //    if (dto.order == DataOrderType.asc)
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenBy(x => x.likesCount).ThenByDescending(x => x.createTime);
                //    }
                //    else
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenByDescending(x => x.likesCount).ThenByDescending(x => x.createTime);
                //    }
                //}
                //if (dto.orderBy == "date")
                //{
                //    if (dto.order == DataOrderType.asc)
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenBy(x => x.createTime);
                //    }
                //    else
                //    {
                //        list = list.OrderByDescending(x => x.post_order).ThenByDescending(x => x.createTime);
                //    }
                //}

                int count = list.Count();

                if (dto.page < 0)
                    dto.page = 1;
                if (dto.pageSize < 0)
                    dto.pageSize = 10;
                list = list.Skip((dto.page - 1) * dto.pageSize).Take(dto.pageSize).ToList();


                return new ResData<object>() { Status = ResStatus.成功, Msg = "sucess", Info = new { count = count, list = list.ToList() } };
            }
            catch (Exception ex)
            {
                return new ResData<object>() { Status = ResStatus.查询失败, Msg = ex.Message, Info = null };
            }
        }

    }
}
